class NetworksController < ApplicationController
  # GET /networks
  # GET /networks.xml
  
  before_filter :login_required
  
  def index
    @title = "Index Networks"
    @networks = Network.all

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @networks }
    end
  end

  # GET /networks/1
  # GET /networks/1.xml
  def show
    @title = "Show Networks"
    @network = Network.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @network }
    end
  end

  # GET /networks/new
  # GET /networks/new.xml
  def new
    @title = "New Networks"
    @network = Network.new
    @computers = Computer.find(:all)

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @network }
    end
  end

  # GET /networks/1/edit
  def edit
    @title = "Edit Networks"
    @computers = Computer.find(:all)
    @network = Network.find(params[:id])
  end

  # POST /networks
  # POST /networks.xml
  def create
  	@title = "Create Networks"
    @network = Network.new(params[:network])
    respond_to do |format|
      if @network.save
        flash[:notice] = 'Network was successfully created.'
        format.html { redirect_to(@network) }
        format.xml  { render :xml => @network, :status => :created, :location => @network }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @network.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /networks/1
  # PUT /networks/1.xml
  def update
    @title = "Update Networks"
    @network = Network.find(params[:id])

    respond_to do |format|
      if @network.update_attributes(params[:network])
        flash[:notice] = 'Network was successfully updated.'
        format.html { redirect_to(@network) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @network.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /networks/1
  # DELETE /networks/1.xml
  def destroy
    @title = "Destroy Networks"
    @network = Network.find(params[:id])
    @network.destroy

    respond_to do |format|
      format.html { redirect_to(networks_url) }
      format.xml  { head :ok }
    end
  end
  
  def add_room
    
    room = Room.find(params[:room_id])
    network = Network.find(params[:network_id])
    network.rooms << room
    
    render :update do |page|
      page.remove "room_#{params[:room_id]}"
      page.insert_html :bottom , 'container_room', :partial => 'networks/network_room', :locals => {:room => room}
    end
 
  end

  def delete_room
    
    room = Room.find(params[:room_id])
    network = Network.find(params[:network_id])
    network.rooms.delete(room)
      
    render :update do |page|
      page.remove "room_#{params[:room_id]}"
      page.insert_html :bottom , 'trash_room', :partial => 'networks/network_room', :locals => {:room => room}
    end
    
  end
  
end
